﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Marmat.Forms
{
    internal class TabFormPanelConverter : StringConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (ReferenceEquals(destinationType, typeof(ArcRadius)))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (ReferenceEquals(destinationType, typeof(string)) && value is ArcRadius)
            {
                ArcRadius so = (ArcRadius)value;
                return so.ToString();
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (ReferenceEquals(sourceType, typeof(string)))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    // Dim s As String = DirectCast(value, String)
                    // Dim colon As Integer = s.IndexOf(":"c)
                    // Dim comma As Integer = s.IndexOf(","c)
                    // If colon <> -1 AndAlso comma <> -1 Then
                    // Dim checkWhileTyping As String = s.Substring(colon + 1, (comma - colon - 1))
                    // colon = s.IndexOf(":"c, comma + 1)
                    // comma = s.IndexOf(","c, comma + 1)
                    // Dim checkCaps As String = s.Substring(colon + 1, (comma - colon - 1))
                    // colon = s.IndexOf(":"c, comma + 1)
                    // Dim suggCorr As String = s.Substring(colon + 1)
                    // 'Dim so As New Data.Binding()
                    // 'so.SpellCheckWhileTyping = [Boolean].Parse(checkWhileTyping)
                    // 'so.SpellCheckCAPS = [Boolean].Parse(checkCaps)
                    // 'so.SuggestCorrections = [Boolean].Parse(suggCorr)
                    // Return "航空件"
                    // End If
                    return "航空件";
                }
                catch
                {
                    throw new ArgumentException("无法将\" & DirectCast(value, String) & \"转换为 SpellingOptions 类型");
                }
            }

            return base.ConvertFrom(context, culture, value);
        }
    }
}